import { ref } from 'vue';

/**
 * 通用的 API 钩子
 * @param {Function} apiFunction - 请求函数
 * @param {Object} params - 请求的参数
 * @returns {Object} 包含响应数据、加载状态、错误信息等
 */
export function useApi(apiFunction, params = {}) {
  const data = ref(null);
  const error = ref(null);
  const isLoading = ref(false);

  const fetchData = async () => {
    isLoading.value = true;
    error.value = null;

    try {
      const response = await apiFunction(params);
      data.value = response.data || response;
    } catch (err) {
      error.value = err;
    } finally {
      isLoading.value = false;
    }
  };

  return {
    data,
    error,
    isLoading,
    fetchData,
  };
}
